/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取用户信息
    var ajax = new $ax(Feng.ctxPath + "/mgr/getUserInfo?userId=" + Feng.getUrlParam("userId"));
    var result = ajax.start();


    $("input[name='userLevel']").each(function () {
        if($(this).val()==result.data.userLevel){
            $(this).attr("checked", "checked");
            return false;
        }
    })

    if(result.data.userLevel==1){
        $(".city").hide();
        $(".county").hide()

        $(".dept").show();
        //获取部门option
        var deptList = [];
        var deptAjax = new $ax(Feng.ctxPath + "/dept/selectChildren",function (data) {
            deptList = data;
        }, function (data) {
            Feng.error("获取部门失败！" + data.responseJSON.message)
        });
        deptAjax.start();
        var deptOption = "<option value=''>选择部门</option>";
        for(var i=0;i<deptList.length;i++){
            deptOption = deptOption + "<option value='"+deptList[i].dept_id+"'>"+deptList[i].simple_name+"</option>";
        }
        $("#deptId").empty();
        $("#deptId").append(deptOption)
        form.render('select');
    }else if(result.data.userLevel==2){
        $(".city").show();
        $(".county").hide()
        $(".dept").hide();
        //查询市
        var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
            $("#city").empty();
            $("#city").append('<option value="">请选择市</option>')
            $("#city").append(data)
        })
        pAjax.set("regionLevel",result.data.userLevel)
        pAjax.set("parentId",16)
        pAjax.start();
        form.render('select');
    }else if(result.data.userLevel==3){
        $(".city").show();
        $(".county").show();
        $(".dept").hide();
        //查询市
        var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
            $("#city").empty();
            $("#city").append('<option value="">请选择市</option>')
            $("#city").append(data)
        })
        pAjax.set("regionLevel",2)
        pAjax.set("parentId",16)
        pAjax.start();

        //查询区
        var qAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
            $("#county").empty();
            $("#county").append('<option value="">请选择区</option>')
            $("#county").append(data)
        })
        qAjax.set("regionLevel",3)
        qAjax.set("parentId",result.data.city)
        qAjax.start();
        form.render('select');
    }

    form.val('userForm', result.data);

    // 点击部门时
    $('#deptName').click(function () {
        var formName = encodeURIComponent("parent.UserInfoDlg.data.deptName");
        var formId = encodeURIComponent("parent.UserInfoDlg.data.deptId");
        var treeUrl = encodeURIComponent("/dept/tree");

        layer.open({
            type: 2,
            title: '部门选择',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                console.log(UserInfoDlg.data);
                $("#deptId").val(UserInfoDlg.data.deptId);
                $("#deptName").val(UserInfoDlg.data.deptName);
            }
        });
    });

    // 添加表单验证方法
    form.verify({
        psw: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repsw: function (value) {
            if (value !== $('#userForm input[name=password]').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    // 渲染时间选择框
    laydate.render({
        elem: '#birthday'
        ,trigger : 'click'
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var level = $("input[name='userLevel']:checked").val();
        if(level==null || level==''){
            Feng.error("请选择用户级别")
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/mgr/edit", function (data) {
            Feng.success("修改成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
            return false
        });
        ajax.set(data.field);
        ajax.start();
    });


    form.on('radio(userLevel)',function (data) {
        var level = data.value;
        if(level==1){
            $(".city").hide();
            $(".county").hide()
            $(".dept").show();
            //获取部门option
            var deptList = [];
            var deptAjax = new $ax(Feng.ctxPath + "/dept/selectChildren",function (data) {
                deptList = data;
            }, function (data) {
                Feng.error("获取部门失败！" + data.responseJSON.message)
            });
            deptAjax.start();
            var deptOption = "<option value=''>选择部门</option>";
            for(var i=0;i<deptList.length;i++){
                deptOption = deptOption + "<option value='"+deptList[i].dept_id+"'>"+deptList[i].simple_name+"</option>";
            }
            $("#deptId").empty();
            $("#deptId").append(deptOption)
            form.render('select');
        }else if(level==2){
            $(".city").show();
            $(".county").hide()
            $(".dept").hide();
            //查询市
            var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
                $("#city").empty();
                $("#city").append('<option value="">请选择市</option>')
                $("#city").append(data)
            })
            pAjax.set("regionLevel",level)
            pAjax.set("parentId",16)
            pAjax.start();
            form.render('select');
        }else if(level==3){
            $(".city").show();
            $(".county").show();
            $(".dept").hide();
            //查询市
            var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
                $("#city").empty();
                $("#city").append('<option value="">请选择市</option>')
                $("#city").append(data)
            })
            pAjax.set("regionLevel",2)
            pAjax.set("parentId",16)
            pAjax.start();
            form.render('select');
        }
    })

    form.on('select(city)', function(data){
        var level = $("input[name='userLevel']:checked").val();
        if(level=="3"){
            var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
                $("#county").empty();
                $("#county").append('<option value="">请选择区</option>')
                $("#county").append(data)
            })
            pAjax.set("regionLevel",level)
            pAjax.set("parentId",data.value)
            pAjax.start();
            form.render('select');
        }
    });
});